﻿using DataViewTool;
using DataViewTool.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace WindowsFormsApplication2
{
    static class Program
    {
        public static readonly string FilePath = @"C:\Windows\数据查看工具.dump";
        /// <summary>
        /// 全局数据源
        /// </summary>
        public static DataMoudels DataMoudels { get; set; }
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Program.Load();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainFrm());
        }

        public static void Save()
        {
            if (DataMoudels != null) {
                using (FileStream fs = new FileStream(FilePath, FileMode.Create))
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    bf.Serialize(fs, Program.DataMoudels);
                    fs.Close();
                }
            }
        }

        public static void Load() {
            if (File.Exists(FilePath))
            {
                using (FileStream fs = new FileStream(FilePath, FileMode.Open))
                {
                    BinaryFormatter binFormat = new BinaryFormatter();
                    Program.DataMoudels = binFormat.Deserialize(fs) as DataMoudels;
                }
            }
            else
            {
                DataMoudels = new DataMoudels();
                DataMoudels.Models = new List<DataModel>();
            }
        }
    }
}
